//排行榜模块
const oneDayTime = 86400000
const yesterdayCount = 20
var rankServer = function(app) {
	this.app = app
	this.channelService = this.app.get('channelService')
}
rankServer.prototype.init = function() {
	var self = this
	var curTime = Date.now()
	var tomorrowDate = new Date(curTime + oneDayTime)
	tomorrowDate.setHours(0,0,0,0)
	var tomorrowTime = tomorrowDate.getTime()
	console.log("时间:",tomorrowTime - curTime)
	setTimeout(function() {
		self.dayTimer.call(self)
		setInterval(self.dayTimer.bind(self),oneDayTime)
	},tomorrowTime - curTime)
}
//每日定时器
rankServer.prototype.dayTimer = function() {
	//清除每日排行榜
	var self = this
	console.log("每日刷新")
	self.redisDao.db.hgetall("gameTypesMap",function(err,data) {
		if(!err && data){
			for(let gameType in data){
				self.redisDao.db.del("yesterdayrank:"+gameType)
				self.getDayRank(gameType,yesterdayCount,function(flag,data) {
					if(flag && data && data.length){
						for(let i = 0;i < data.length;i += 2){
							self.redisDao.db.zincrby("yesterdayrank:"+gameType,data[i + 1],data[i])
						}
					}
					console.log("dayrank:"+gameType)
					self.redisDao.db.del("dayrank:"+gameType)
				})
			}
		}
		self.redisDao.db.del("gameTypesMap")
	})
}
//改变日排行榜分数
rankServer.prototype.changeDayRank = function(ustr,gameType,score,cb) {
	if(typeof(ustr) != "string" || typeof(gameType) != "string" || typeof(score) != "number"){
		cb(false,"参数错误")
		return
	}
	this.redisDao.db.hincrby("gameTypesMap",gameType,1)
	this.redisDao.db.zincrby("dayrank:"+gameType,score,ustr,function(err,data) {
		if(err){
			cb(false,err)
		}else{
			cb(true,data)
		}
	})
}
//获取昨日排行榜
rankServer.prototype.getYesterdayRank = function(gameType,count,cb) {
	if(typeof(gameType) != "string" || typeof(count) != "number"){
		cb(false,"参数错误")
		return
	}
	if(count >= 100){
		count = 100
	}
	this.redisDao.db.zrange("yesterdayrank:"+gameType,-count,-1,"withscores",function(err,data) {
		if(err){
			cb(false,err)
		}else{
			cb(true,data)
		}
	})
}
//获取今日排行榜
rankServer.prototype.getDayRank = function(gameType,count,cb) {
	if(typeof(gameType) != "string" || typeof(count) != "number"){
		cb(false,"参数错误")
		return
	}
	if(count >= 100){
		count = 100
	}
	this.redisDao.db.zrange("dayrank:"+gameType,-count,-1,"withscores",function(err,data) {
		if(err){
			cb(false,err)
		}else{
			cb(true,data)
		}
	})
}
//改变持久排行榜分数
rankServer.prototype.changeDurableRank = function(ustr,gameType,score,cb) {
	if(typeof(ustr) != "string" || typeof(gameType) != "string" || typeof(score) != "number"){
		cb(false,"参数错误")
		return
	}
	this.redisDao.db.zincrby("durablerank:"+gameType,score,ustr,function(err,data) {
		if(err){
			cb(false,err)
		}else{
			cb(true,data)
		}
	})
}
//获取持久排行榜
rankServer.prototype.getDurableRank = function(gameType,count,cb) {
	if(typeof(gameType) != "string" || typeof(count) != "number"){
		cb(false,"参数错误")
		return
	}
	if(count >= 100){
		count = 100
	}
	this.redisDao.db.zrange("durablerank:"+gameType,-count,-1,"withscores",function(err,data) {
		if(err){
			cb(false,err)
		}else{
			cb(true,data)
		}
	})
}
module.exports = {
	id : "rankServer",
	func : rankServer,
	scope : "prototype",
	init : "init",
	args : [{
		name : "app",
		type : "Object"
	}],
	props : [{
		name : "redisDao",
		ref : "redisDao"
	}]
}